package com.tanhua.server.service;

import com.tanhua.commons.templates.HuanXinTemplate;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.ContactVo;
import com.tanhua.domain.vo.MessageVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.server.interceptors.UserHolder;
import com.tanhua.server.utils.RelativeDateFormat;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class IMService {

    @Reference
    private FriendApi friendApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private CommentApi commentApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;
    /**
     * 添加联系人
     * @param paramMap
     */
    public void addContacts(Map<String, Long> paramMap) {
        // 佳人id
        Long userId = paramMap.get("userId");
        //1. 添加为好友，添加记录到user_friend表，双向添加
        friendApi.makeFriends(UserHolder.getUserId(), userId);
        //2. 在环信上注册为好友
        huanXinTemplate.makeFriends(UserHolder.getUserId(), userId);
    }

    /**
     * 联系人分页查询
     * @param page
     * @param pageSize
     * @param keyword
     * @return
     */
    public PageResult contactList(Long page, Long pageSize, String keyword) {
        //1. 分页查询登录用户的好友
        PageResult pageResult = friendApi.findPage(UserHolder.getUserId(), page, pageSize);
        //1.1 获取分页结果集
        List<Friend> friendList = pageResult.getItems();
        List<ContactVo> voList = new ArrayList<>();
        //1.2 结果集非空判断
        if(!CollectionUtils.isEmpty(friendList)) {
            //2. 查询好友的信息
            //2.1 取出所有好友的id
            List<Long> userIds = friendList.stream().map(Friend::getFriendId).collect(Collectors.toList());
            //2.2 批量查询
            List<UserInfo> userInfoList = userInfoApi.findByBatchId(userIds);
            //2.3 转成map
            Map<Long, UserInfo> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, u -> u));
            //3. 转成vo
            //3.1 遍历好友结果集
            for (Friend friend : friendList) {
                //3.2 构建vo, 复制属性
                ContactVo vo = new ContactVo();
                //取出好友信息
                UserInfo userInfo = userInfoMap.get(friend.getFriendId());
                BeanUtils.copyProperties(userInfo,vo);
                vo.setUserId(friend.getFriendId().toString());
                //3.3 添加到voList
                voList.add(vo);
            }
            //3.4 设置回pageResult
            pageResult.setItems(voList);
        }
        //4. 返回pageResult
        return pageResult;
    }

    /**
     * 谁点赞、喜欢、评论了我 列表
     * @param commentType //评论类型，1-点赞，2-评论，3-喜欢
     * @param page
     * @param pageSize
     * @return
     */
    public PageResult messageCommentList(int commentType, Long page, Long pageSize) {
        //1. 分页查询谁评论、点赞、喜欢了我
        PageResult pageResult = commentApi.findPageByCommentType(UserHolder.getUserId(), commentType, page, pageSize);
        //2. 得到分页的结果集
        List<Comment> commentList = pageResult.getItems();
        List<MessageVo> voList = new ArrayList<>();
        //3. 取所的操作人的id
        if(!CollectionUtils.isEmpty(commentList)) {
            List<Long> userIds = commentList.stream().map(Comment::getUserId).collect(Collectors.toList());
            //4. 批量查询操作人信息
            List<UserInfo> userInfoList = userInfoApi.findByBatchId(userIds);
            Map<Long, UserInfo> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, u -> u));
            //5. 转成vo
            for (Comment comment : commentList) {
                MessageVo vo = new MessageVo();
                // 操作人的信息
                UserInfo userInfo = userInfoMap.get(comment.getUserId());
                BeanUtils.copyProperties(userInfo, vo);
                vo.setId(comment.getId().toHexString());
                vo.setCreateDate(RelativeDateFormat.format(new Date(comment.getCreated())));
                voList.add(vo);
            }
        }
        pageResult.setItems(voList);
        //6. 返回
        return pageResult;
    }
}
